import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {
        path:"/",
        redirect:"/users"
      },
      {
        path:"/users",
        component:()=>import('../components/Users.vue')
      },
      {
        path:"/roles",
        component:()=>import('../components/Roles.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  routes
})
// 路由守卫
router.beforeEach((to,from,next)=>{
  var token=sessionStorage.getItem("token");
  // 判断token是否为空
  if(!token){
    if(to.path!='/login'){
      next("/login")
    }else{
      next();
    }
  }else{
    next();
  }
})

export default router
